﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.RolesService;
using WebSiteAdminTool.UI.Ioc;
using WebSiteAdminTool.UI.ViewModels.Security.Roles;
using WebSiteAdminTool.UI.Services.Messaging;
using WebSiteAdminTool.UI.Services.Messaging.DataManipulationMessages;
using WebSiteAdminTool.UI.Utils;
using WebSiteAdminTool.UI.MembershipService;
using WebSiteAdminTool.UI.ViewModels.Security.Users;
using WebSiteAdminTool.UI.Services.MessageBoxes;

namespace WebSiteAdminTool.UI.ViewModels.Security
{
    public static class SecurityUiHelpers
    {
        #region role methods

        public static void ManageRoleDialog(RoleData role = null)
        {
            var dialog = Factory.ViewLocator.ManageRoleModalDialogView();
            var viewModel = Factory.ViewModelLocator.ManageRoleViewModel(role);
            Factory.ServiceLocator.ModalDialogService.ShowDialog<ManageRoleViewModel>(dialog, viewModel);
        }

        public static void RemoveRole(RoleData role)
        {
            if (null != role &&
                 GenericMessageBoxResult.Ok == Factory.ServiceLocator.MessageBoxService.Show("Are you sure to remove selected role?\nThis can affect users associated with it.", "Remove Role", Services.MessageBoxes.GenericMessageBoxButton.OkCancel))
            {
                var client = new RolesServiceClient();
                client.RemoveRoleCompleted += new EventHandler<RemoveRoleCompletedEventArgs>(client_RemoveRoleCompleted);
                client.RemoveRoleAsync(role.RoleId, role);
            }
        }

        public static void ManageRoleUsers(RoleData role)
        {
        }

        static void client_RemoveRoleCompleted(object sender, RemoveRoleCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && e.Result)
            {
                MessageManager.SendMessage<DataObjectDeleted<RoleData>>(new DataObjectDeleted<RoleData>(e.UserState as RoleData));
            }
        }

        #endregion

        #region user methods

        public static void CreateUserDialog()
        {
            var dialog = Factory.ViewLocator.CreateUserModalDialogView();
            var viewModel = Factory.ViewModelLocator.CreateUserViewModel();
            Factory.ServiceLocator.ModalDialogService.ShowDialog<CreateUserViewModel>(dialog, viewModel);
        }

        public static void EditUserDialog(UserData user)
        {
            var dialog = Factory.ViewLocator.EditUserModalDialogView();
            var viewModel = Factory.ViewModelLocator.EditUserViewModel(user);
            Factory.ServiceLocator.ModalDialogService.ShowDialog<EditUserViewModel>(dialog, viewModel);
        }

        public static void RemoveUser(UserData user)
        {
            if (null != user &&
                 GenericMessageBoxResult.Ok == Factory.ServiceLocator.MessageBoxService.Show("Are you sure to remove selected user?", "Remove User", Services.MessageBoxes.GenericMessageBoxButton.OkCancel))
            {
                var client = new MembershipServiceClient();
                client.RemoveUserCompleted += new EventHandler<RemoveUserCompletedEventArgs>(client_RemoveUserCompleted);
                // TODO: ask about data deletion
                client.RemoveUserAsync(user.UserName, false, user);
            }
        }

        static void client_RemoveUserCompleted(object sender, RemoveUserCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && e.Result)
            {
                MessageManager.SendMessage<DataObjectDeleted<UserData>>(new DataObjectDeleted<UserData>(e.UserState as UserData));
            }
        }

        #endregion
    }
}
